<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>问卷测评演示v3</title>
    <!-- 引入TailwindCSS CDN -->
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        /* 自定义样式 */
        body {
            font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
            background-color: #f5f7fa;
        }

        .camera-container {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1000;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            transition: all 0.3s ease;
            width: 256px;
            height: 192px;
            cursor: move; /* 添加移动光标样式 */
        }

        .camera-container.hidden {
            display: none;
        }

        .iframe-container {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 900;
            display: none;
        }

        .iframe-container.active {
            display: block;
        }

        .btn-primary {
            background: linear-gradient(135deg, #4f46e5, #6366f1);
            transition: all 0.3s ease;
        }

        .btn-primary:hover {
            background: linear-gradient(135deg, #4338ca, #4f46e5);
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
        }

        .camera-placeholder {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #e5e7eb;
            color: #6b7280;
        }

        .close-btn {
            position: absolute;
            top: 5px;
            right: 5px;
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            border-radius: 50%;
            width: 24px;
            height: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            z-index: 1001;
        }

        #cameraView {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
    </style>
</head>
<body class="min-h-screen flex flex-col items-center justify-center p-4">
    <div class="max-w-md w-full bg-white rounded-xl shadow-lg p-8 mb-8">
        <h1 class="text-3xl font-bold text-center text-gray-800 mb-6">问卷测评系统</h1>
        <p class="text-gray-600 mb-8 text-center">欢迎参与我们的问卷测评，点击下方按钮开始测评过程。</p>

        <div class="flex justify-center">
            <button id="startBtn" class="btn-primary text-white font-medium py-3 px-8 rounded-lg focus:outline-none">
                开始问卷测评
            </button>
        </div>
    </div>

    <!-- 摄像头容器 -->
    <div id="cameraContainer" class="camera-container hidden">
        <div class="close-btn" id="closeCamera">×</div>
        <div id="cameraPlaceholder" class="camera-placeholder">
            <span>等待摄像头权限...</span>
        </div>
        <video id="cameraView" autoplay playsinline class="hidden"></video>
    </div>

    <!-- iframe容器 -->
    <div id="iframeContainer" class="iframe-container">
        <iframe id="questionnaireFrame" src="" frameborder="0" class="w-full h-full"></iframe>
    </div>

    <script>
        // 获取DOM元素
        const startBtn = document.getElementById('startBtn');
        const cameraContainer = document.getElementById('cameraContainer');
        const cameraPlaceholder = document.getElementById('cameraPlaceholder');
        const cameraView = document.getElementById('cameraView');
        const iframeContainer = document.getElementById('iframeContainer');
        const questionnaireFrame = document.getElementById('questionnaireFrame');
        const closeCamera = document.getElementById('closeCamera');

        // 问卷URL（示例URL，实际使用时替换）
        const questionnaireUrl = 'https://psytest.ttmm.vip/storage/extracted-questionnaire/20250312093158_PX1XLyMKXO/index.html';

        // 实现拖动功能
        let isDragging = false;
        let offsetX, offsetY;

        // 鼠标按下事件
        cameraContainer.addEventListener('mousedown', (e) => {
            // 如果点击的是关闭按钮，不启动拖动
            if (e.target === closeCamera) {
                return;
            }

            isDragging = true;

            // 计算鼠标在容器内的相对位置
            const rect = cameraContainer.getBoundingClientRect();
            offsetX = e.clientX - rect.left;
            offsetY = e.clientY - rect.top;

            // 防止拖动时选中文本
            e.preventDefault();
        });

        // 鼠标移动事件
        document.addEventListener('mousemove', (e) => {
            if (!isDragging) return;

            // 计算新位置
            const newLeft = e.clientX - offsetX;
            const newTop = e.clientY - offsetY;

            // 确保不超出屏幕边界
            const maxX = window.innerWidth - cameraContainer.offsetWidth;
            const maxY = window.innerHeight - cameraContainer.offsetHeight;

            // 设置新位置
            cameraContainer.style.left = `${Math.max(0, Math.min(maxX, newLeft))}px`;
            cameraContainer.style.top = `${Math.max(0, Math.min(maxY, newTop))}px`;
            cameraContainer.style.right = 'auto'; // 取消右侧定位
        });

        // 鼠标松开事件
        document.addEventListener('mouseup', () => {
            isDragging = false;
        });

        // 触摸设备支持
        cameraContainer.addEventListener('touchstart', (e) => {
            // 如果点击的是关闭按钮，不启动拖动
            if (e.target === closeCamera) {
                return;
            }

            isDragging = true;

            const touch = e.touches[0];
            const rect = cameraContainer.getBoundingClientRect();
            offsetX = touch.clientX - rect.left;
            offsetY = touch.clientY - rect.top;

            e.preventDefault();
        });

        document.addEventListener('touchmove', (e) => {
            if (!isDragging) return;

            const touch = e.touches[0];
            const newLeft = touch.clientX - offsetX;
            const newTop = touch.clientY - offsetY;

            const maxX = window.innerWidth - cameraContainer.offsetWidth;
            const maxY = window.innerHeight - cameraContainer.offsetHeight;

            cameraContainer.style.left = `${Math.max(0, Math.min(maxX, newLeft))}px`;
            cameraContainer.style.top = `${Math.max(0, Math.min(maxY, newTop))}px`;
            cameraContainer.style.right = 'auto';

            e.preventDefault();
        });

        document.addEventListener('touchend', () => {
            isDragging = false;
        });

        // 开始测评按钮点击事件
        startBtn.addEventListener('click', async () => {
            try {
                // 显示摄像头容器
                cameraContainer.classList.remove('hidden');

                // 请求摄像头权限
                const stream = await navigator.mediaDevices.getUserMedia({
                    video: {
                        width: 256,
                        height: 192,
                        facingMode: 'user'
                    },
                    audio: false
                });

                // 权限获取成功，显示摄像头画面
                cameraView.srcObject = stream;
                cameraPlaceholder.classList.add('hidden');
                cameraView.classList.remove('hidden');

                // 打开问卷iframe
                questionnaireFrame.src = questionnaireUrl;
                iframeContainer.classList.add('active');

            } catch (err) {
                // 处理权限被拒绝的情况
                console.error('摄像头权限获取失败:', err);
                cameraPlaceholder.innerHTML = '<span class="text-red-500">摄像头权限被拒绝</span>';
            }
        });

        // 关闭摄像头按钮点击事件
        closeCamera.addEventListener('click', () => {
            // 停止摄像头流
            if (cameraView.srcObject) {
                const tracks = cameraView.srcObject.getTracks();
                tracks.forEach(track => track.stop());
            }

            // 隐藏摄像头容器
            cameraContainer.classList.add('hidden');

            // 重置摄像头视图
            cameraView.classList.add('hidden');
            cameraPlaceholder.classList.remove('hidden');
            cameraPlaceholder.innerHTML = '<span>等待摄像头权限...</span>';

            // 关闭iframe
            iframeContainer.classList.remove('active');
            questionnaireFrame.src = '';
        });

        // 添加响应式调整
        window.addEventListener('resize', () => {
            // 根据窗口大小调整摄像头容器大小
            if (window.innerWidth < 640) { // 移动设备
                cameraContainer.style.width = '120px';
                cameraContainer.style.height = '90px';
            } else { // 桌面设备
                cameraContainer.style.width = '256px';
                cameraContainer.style.height = '192px';
            }
        });

        // 初始调整
        if (window.innerWidth < 640) {
            cameraContainer.style.width = '120px';
            cameraContainer.style.height = '90px';
        }
    </script>
</body>
</html>
